<template>
  <van-pull-refresh
    v-model="isLoading"
    @refresh="onRefresh"
    :success-text="$t('m.sxcg')"
    style="min-height: 100vh"
  >
    <div id="name">
      <div class="wl-body">
        <div>
          <div class="dh-top">
            <div class="dh-tops">
              <span class="ye"
                >{{ $t("m.zhye") }} :
                {{
                  changer
                    ? (s ? s.ftAmount : "0").toFixed(4) + "TROT"
                    : (s ? s.trxAmount : 0).toFixed(4) + "USDT"
                }}</span>
                <span class="gotrx" @click="goto('zcusdt')">去充值</span>
            </div>
            <div class="dh-bodys">
              <div :class="[changer ? 'dh-left' : 'dh-left-re']">
                <div class="dh-left-item" :class="[changer ? '' : 'nos']">
                  <img src="../assets/timg/tot.png" alt="" class="btion" />
                  <div>TROT</div>
                </div>
                <div class="dh-left-item" :class="[changer ? 'nos' : '']">
                  <img src="../assets/timg/usdt.png" alt="" class="btion" />
                  <div>USDT</div>
                </div>
              </div>
              <div class="ddbtn">
                <div class="lin-top"></div>
                <img src="../assets/timg/zhbtn.png" alt="" class="btion" />
                <div class="lin-bottom"></div>
              </div>
              <div class="dh-right">
                <div class="dh-right-item">
                  <div class="t-left">{{ $t("m.zf") }}</div>
                  <div>
                    <input
                      type="number"
                      class="yyt nos"
                      v-model="sde"
                      :placeholder="$t('m.zfsl')"
                      @input="ssde"
                      @keydown="gs"
                    />
                  </div>
                </div>
                <div class="dh-right-item nos">
                  <div class="t-left">{{ $t("m.jies") }}</div>
                  <div>
                    <input
                      type="number"
                      class="yyt nos"
                      v-model="dhui"
                      :placeholder="$t('m.jiessl')"
                      @input="ssder"
                      @keydown="gs"
                    />
                  </div>
                </div>
              </div>
            </div>
            <div class="dh-footer">
              <span>{{ $t("m.dqhl") }} ：</span
              ><span v-if="changer"
                >{{ 1 }} TROT≈{{ (1 * base).toFixed(4) }} USDT</span
              ><span v-else>1 USDT ≈{{ (1 / base).toFixed(4) }} TROT</span>
            </div>
          </div>
          <div class="btns" @click="exchange()">{{ $t("m.dh") }}</div>
          <div>
            <div>
              <div class="i-title t-left mtp">— {{ $t("m.jllb") }} —</div>
            </div>

            <div class="bdds slideIn" v-if="show">
              <van-list
                v-model="loading"
                :finished="finished"
                :finished-text="$t('m.nomore')"
                @load="onLoad"
              >
                <div
                  class="link-item bottml"
                  v-for="(item, k) in lslist"
                  :key="k"
                >
                  <div class="slink defcf">
                    <div class="flex1">
                      {{ item.type == 1 ? "USDT" : "TROT" }}
                    </div>
                    <div class="flex1">
                      {{ item.type == 1 ? "TROT" : "USDT" }}
                    </div>
                    <div class="flex1">{{ $t("m.sj") }}</div>
                  </div>
                  <div class="slink u-dj">
                    <div class="flex1">
                      {{ item.type == 1 ? item.amountU : item.amountT }}
                    </div>
                    <div class="flex1">
                      {{ item.type == 1 ? item.amountT : item.amountU }}
                    </div>
                    <div class="flex1">{{ item.createDate }}</div>
                  </div>
                </div>
              </van-list>
            </div>
          </div>
        </div>
      </div>
    </div>
  </van-pull-refresh>
</template>

<script>
export default {
  name: "name",
  data() {
    return {
      sdshow: false,
      isLoading: false,
      type: "",
      s: "",
      see: "",
      base:this.$store.getters.ttou,
      sde: "",
      dhui: "",
      suoding: false,
      changer: false,
      show: true,
      value: "",
      lslist: [],
      loading: false,
      finished: false,
      pageNum: 1,
    };
  },
  created() {
    this.getdata();
    this.getbs();
    this.s = JSON.parse(sessionStorage.getItem("s"));
    if (this.$store.getters.see == "0") {
      this.see = false;
    } else {
      this.see = true;
    }
  },
  computed: {
    // base() {
    //   console.log("ttou", this.$store.getters.ttou);
    //   return this.$store.getters.ttou;
    // },
    dqyy() {
      return this.$store.getters.language;
    },
  },
  methods: {
    onRefresh() {
      setTimeout(() => {
        this.isLoading = false;
        this.getdata();
        this.getbs();
      }, 1000);
    },
    guanbi() {
      this.$router.go(-1);
    },
    changers() {
      this.changer = !this.changer;
      this.ssde(this.sde);
      this.ssder(this.dhui);
    },
    gs(e) {
      e.target.value = e.target.value.match(/^\d*(\.?\d{0,5})/g)[0] || null;
    },
    ssde() {
      if (this.changer) {
        if (this.sde > this.s.ftAmount) {
          this.sde = this.s.ftAmount;
        }
        this.dhui = (this.sde * this.base).toFixed(4);
      } else {
        if (this.sde > this.s.trxAmount) {
          this.sde = this.s.trxAmount;
        }
        this.dhui = (this.sde / this.base).toFixed(4);
      }
    },
    ssder() {
      if (this.changer) {
        console.log(1);

        this.sde = (this.dhui / this.base).toFixed(4);
        if (this.sde > this.s.ftAmount) {
          this.sde = this.s.ftAmount;
          this.dhui = (this.sde * this.base).toFixed(4);
        }
      } else {
        console.log(2);
        this.sde = (this.dhui * this.base).toFixed(4);
        if (this.sde > this.s.trxAmount) {
          this.sde = this.s.trxAmount;
          this.dhui = (this.sde / this.base).toFixed(4);
        }
      }
    },
    goto(val){
      this.$router.push({ path: val });
    },
  getbs() {
      this.$get({
        url: "wms-wallet/getExchangeRate",
        data: {},
        callback: (data) => {
          this.$nextTick(function () {
            var s = data.data;
            if (s) {
             this.base=s
            }
          });
        },
      }).catch(() => {
        this.$toast(this.$i18n.t("m.errordata"));
      });
    },
    getbss() {
      this.$get({
        url: "oms-ore-pool/get0Rate",
        data: {},
        callback: (data) => {
          this.$nextTick(function () {
            var s = data.data;
            if (s) {
              if (this.dqyy == "en-US") {
                this.$store.commit("ttoc", s.token_usdt);
                this.$store.commit("utoc", s.trx_usdt);
                this.$store.commit("ttou", s.token_trx);
              } else {
                this.$store.commit("ttoc", s.token_cny);
                this.$store.commit("utoc", s.trx_cny);
                this.$store.commit("ttou", s.token_trx);
              }
            }
          });
        },
      }).catch(() => {
        this.$toast(this.$i18n.t("m.errordata"));
      });
    },

    exchange() {
      if (this.dhui == 0 || this.sde == 0) {
        this.$toast(this.$i18n.t("m.dhslshr"));
        return;
      }
      if (this.suoding) {
        return;
      } else {
        this.suoding = true;

        var amountU, amountT, type;
        if (this.changer) {
          amountU = this.dhui;
          amountT = this.sde;
          type = 2;
        } else {
          amountU = this.sde;
          amountT = this.dhui;
          type = 1;
        }
        this.$dialog
          .confirm({
            title: this.$i18n.t("m.txt"),
            message: this.$i18n.t("m.qrdh"),
            confirmButtonText: this.$i18n.t("m.qding"),
            cancelButtonText: this.$i18n.t("m.gb"),
          })
          .then(() => {
            // on confirm
            if (this.$store.getters.nameid) {
              let datas = {
                uid: this.$store.getters.nameid,
                amountU: parseFloat(amountU),
                amountT: parseFloat(amountT),
                rate: this.base,
                type: type,
              };
              this.$post({
                url: "wms-wallet/exchange",
                data: datas,
                callback: (data) => {
                  this.$nextTick(function () {
                    if (data.data) {
                      this.$toast(this.$i18n.t("m.dhcg"));
                      this.getdata();
                      this.show = false;
                      this.lslist = [];
                      this.sde = "";
                      this.dhui = "";
                      this.pageNum = 1;
                      this.onLoad();
                    } else {
                      this.$toast(this.$i18n.t("m.dhsb"));
                      this.suoding = false;
                    }
                  });
                },
              }).catch(() => {
                this.$toast(this.$i18n.t("m.errordata"));
              });
            } else {
              this.getuid();
            }
          })
          .catch(() => {
            // on cancel
            this.suoding = false;
          });
      }
    },
    getuid() {
      this.$get({
        url: "wms-wallet/user-game-info/" + this.$store.getters.userWallet,
        data: {},
        callback: (data) => {
          this.$nextTick(function () {
            if (data.data) {
              this.$store.commit("nameid", data.data.id);
              this.onLoad();
            }
          });
        },
      }).catch(() => {
        this.$toast(this.$i18n.t("m.errordata"));
      });
    },
    onLoad() {
      if (this.$store.getters.nameid) {
        var datas = {
          params: {
            uid: this.$store.getters.nameid,
            pageSize: 10,
            pageNum: this.pageNum,
            type: 0,
          },
        };
        this.$get({
          url: "wms-wallet-history/getExchangeHistory",
          data: datas,
          callback: (data) => {
            this.$nextTick(function () {
              var list = data.data;
              if (list) {
                this.show = true;
                if (this.pageNum > 1) {
                  this.lslist = [...this.lslist, ...list];
                } else {
                  this.lslist = list;
                }
                this.loading = false;
                if (list.length != 0) {
                  this.pageNum++;
                } else {
                  this.finished = true;
                }
              }
            });
          },
        }).catch(() => {
          this.$toast(this.$i18n.t("m.errordata"));
        });
      } else {
        this.getdata();
        this.getuid();
      }
    },
    getdata() {
      let datas = {
        params: {
          address: this.$store.getters.userWallet,
        },
      };
      this.$get({
        url: "wms-wallet/getWallet",
        data: datas,
        callback: (data) => {
          this.$nextTick(function () {
            this.s = data.data;
            if (this.s) {
              this.suoding = false;
              this.$store.commit("nameid", this.s.id);
              this.$store.commit("s", this.s);
            }
          });
        },
      }).catch(() => {
        this.$toast(this.$i18n.t("m.errordata"));
      });
    },
  },
};
</script>

<style lang="scss" scoped>
</style>